package com.hotel.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotel.common.Result;
import com.hotel.dto.ReservationDTO;
import com.hotel.entity.Reservation;
import com.hotel.service.ReservationService;
import com.hotel.vo.ReservationVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 预订管理控制器
 * 
 * @author Hotel Management Team
 */
@RestController
@RequestMapping("/reservation")
@RequiredArgsConstructor
@CrossOrigin(origins = "*")
public class ReservationController {

    private final ReservationService reservationService;

    /**
     * 分页查询预订列表
     */
    @GetMapping("/list")
    public Result<Page<ReservationVO>> getReservationPage(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "20") int size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String checkinDate,
            @RequestParam(required = false) String checkoutDate) {
        Page<ReservationVO> page = reservationService.getReservationPage(current, size, keyword, status, checkinDate, checkoutDate);
        return Result.success(page);
    }

    /**
     * 获取预订详情
     */
    @GetMapping("/{id}")
    public Result<ReservationVO> getReservationById(@PathVariable Long id) {
        ReservationVO reservation = reservationService.getReservationById(id);
        return Result.success(reservation);
    }

    /**
     * 根据预订号获取预订
     */
    @GetMapping("/number/{reservationNo}")
    public Result<ReservationVO> getReservationByNumber(@PathVariable String reservationNo) {
        ReservationVO reservation = reservationService.getReservationByNumber(reservationNo);
        return Result.success(reservation);
    }

    /**
     * 创建预订
     */
    @PostMapping
    public Result<Void> createReservation(@Valid @RequestBody ReservationDTO reservationDTO) {
        boolean success = reservationService.createReservation(reservationDTO);
        if (success) {
            return Result.success("预订创建成功");
        } else {
            return Result.error("预订创建失败");
        }
    }

    /**
     * 更新预订
     */
    @PutMapping("/{id}")
    public Result<Void> updateReservation(@PathVariable Long id, @Valid @RequestBody ReservationDTO reservationDTO) {
        boolean success = reservationService.updateReservation(id, reservationDTO);
        if (success) {
            return Result.success("预订更新成功");
        } else {
            return Result.error("预订更新失败");
        }
    }

    /**
     * 取消预订
     */
    @PutMapping("/{id}/cancel")
    public Result<Void> cancelReservation(@PathVariable Long id) {
        boolean success = reservationService.cancelReservation(id);
        if (success) {
            return Result.success("预订取消成功");
        } else {
            return Result.error("预订取消失败");
        }
    }

    /**
     * 确认预订
     */
    @PutMapping("/{id}/confirm")
    public Result<Void> confirmReservation(@PathVariable Long id) {
        boolean success = reservationService.confirmReservation(id);
        if (success) {
            return Result.success("预订确认成功");
        } else {
            return Result.error("预订确认失败");
        }
    }

    /**
     * 办理入住
     */
    @PutMapping("/{id}/checkin")
    public Result<Void> checkin(@PathVariable Long id, @RequestParam Long roomId) {
        boolean success = reservationService.checkin(id, roomId);
        if (success) {
            return Result.success("入住办理成功");
        } else {
            return Result.error("入住办理失败");
        }
    }

    /**
     * 办理退房
     */
    @PutMapping("/{id}/checkout")
    public Result<Void> checkout(@PathVariable Long id) {
        boolean success = reservationService.checkout(id);
        if (success) {
            return Result.success("退房办理成功");
        } else {
            return Result.error("退房办理失败");
        }
    }

    /**
     * 获取客户预订记录
     */
    @GetMapping("/customer/{customerId}")
    public Result<List<ReservationVO>> getReservationsByCustomer(@PathVariable Long customerId) {
        List<ReservationVO> reservations = reservationService.getReservationsByCustomer(customerId);
        return Result.success(reservations);
    }

    /**
     * 获取今日入住预订
     */
    @GetMapping("/today/checkin")
    public Result<List<ReservationVO>> getTodayCheckinReservations() {
        List<ReservationVO> reservations = reservationService.getTodayCheckinReservations();
        return Result.success(reservations);
    }

    /**
     * 获取今日退房预订
     */
    @GetMapping("/today/checkout")
    public Result<List<ReservationVO>> getTodayCheckoutReservations() {
        List<ReservationVO> reservations = reservationService.getTodayCheckoutReservations();
        return Result.success(reservations);
    }

    /**
     * 获取预订统计信息
     */
    @GetMapping("/stats")
    public Result<Map<String, Object>> getReservationStats() {
        Map<String, Object> stats = reservationService.getReservationStats();
        return Result.success(stats);
    }

    /**
     * 获取预订趋势数据
     */
    @GetMapping("/trend")
    public Result<List<Map<String, Object>>> getReservationTrend(@RequestParam(defaultValue = "30") int days) {
        List<Map<String, Object>> trend = reservationService.getReservationTrend(days);
        return Result.success(trend);
    }

    /**
     * 获取入住率
     */
    @GetMapping("/occupancy-rate")
    public Result<Map<String, Object>> getOccupancyRate(@RequestParam(defaultValue = "30") int days) {
        Map<String, Object> occupancyRate = reservationService.getOccupancyRate(days);
        return Result.success(occupancyRate);
    }

    /**
     * 延长住宿
     */
    @PutMapping("/{id}/extend")
    public Result<Void> extendStay(@PathVariable Long id, @RequestParam String newCheckoutDate) {
        boolean success = reservationService.extendStay(id, newCheckoutDate);
        if (success) {
            return Result.success("住宿延长成功");
        } else {
            return Result.error("住宿延长失败");
        }
    }

    /**
     * 更换房间
     */
    @PutMapping("/{id}/change-room")
    public Result<Void> changeRoom(@PathVariable Long id, @RequestParam Long newRoomId) {
        boolean success = reservationService.changeRoom(id, newRoomId);
        if (success) {
            return Result.success("房间更换成功");
        } else {
            return Result.error("房间更换失败");
        }
    }

    /**
     * 获取即将到期的预订
     */
    @GetMapping("/expiring")
    public Result<List<ReservationVO>> getExpiringReservations(@RequestParam(defaultValue = "3") int days) {
        List<ReservationVO> reservations = reservationService.getExpiringReservations(days);
        return Result.success(reservations);
    }

    /**
     * 批量确认预订
     */
    @PutMapping("/batch-confirm")
    public Result<Void> batchConfirmReservations(@RequestBody List<Long> reservationIds) {
        boolean success = reservationService.batchConfirmReservations(reservationIds);
        if (success) {
            return Result.success("批量确认成功");
        } else {
            return Result.error("批量确认失败");
        }
    }

    /**
     * 生成预订号
     */
    @GetMapping("/generate-number")
    public Result<String> generateReservationNumber() {
        String reservationNumber = reservationService.generateReservationNumber();
        return Result.success(reservationNumber);
    }
}